from django.conf import settings
from fdfs_client.client import Fdfs_client
from rest_framework.viewsets import ModelViewSet
from meiduo_admin.my_paginations import MyPageNumberPagination
from meiduo_admin.goods import spu_serializers
from goods.models import SPU, SPUSpecification, Brand, GoodsCategory, SpecificationOption
from rest_framework.decorators import action
from rest_framework.response import Response
from .spu_serializers import SpecsSerializer, BrandSerializer, SpecOptionsSerializer, SpecsSimpleSerializer
from .sku_serializers import GoodsCategorySerializer
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView


class SPUViewSet(ModelViewSet):
    pagination_class = MyPageNumberPagination
    serializer_class = spu_serializers.SPUSerializer
    queryset = SPU.objects.all()

    @action(methods=['get'], detail=False)
    def simple(self, request):
        self.pagination_class = None
        serializer = self.get_serializer(instance=self.get_queryset().all(), many=True)
        return Response(serializer.data, status=200)

    @action(methods=['get'], detail=True)
    def specs(self, request, pk):
        spu = self.get_object()
        spu_specs = SPUSpecification.objects.filter(spu=spu)

        spec_serializer = SpecsSerializer(instance=spu_specs, many=True)
        return Response(spec_serializer.data, status=200)


# get all brand
class BrandSimpleView(ListAPIView):
    serializer_class = BrandSerializer
    queryset = Brand.objects.all()


# get category channel
class ParentCategoryView(ListAPIView):
    serializer_class = GoodsCategorySerializer
    queryset = GoodsCategory.objects.filter(parent=None)


# get sub category
class SubCategoryView(ListAPIView):
    serializer_class = GoodsCategorySerializer

    def get_queryset(self):
        parent_category_id = self.kwargs['cate_id']
        parent_category = GoodsCategory.objects.get(id=parent_category_id)
        return GoodsCategory.objects.filter(parent=parent_category)


class SPUDetailImageView(APIView):
    def post(self, request):
        image = request.FILES.get('image')

        if not image:
            return Response('missing argument', status=400)

        client = Fdfs_client(settings.CONFIG_URL)
        result = client.upload_by_buffer(image.read())

        if result['Status'] != 'Upload successed.':
            return Response('upload failed', status=400)

        img_url = settings.BASE_URL + result['Remote file_id']
        return Response({'img_url': img_url}, status=201)


class SpecificationViewSet(ModelViewSet):
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecsSerializer
    pagination_class = MyPageNumberPagination

    @action(methods=['get'], detail=False)
    def simple(self, request):
        self.pagination_class = None
        serializer = self.get_serializer(instance=self.queryset.all(), many=True)
        return Response(serializer.data, status=200)


class SpecOptionViewSet(ModelViewSet):
    queryset = SpecificationOption.objects.all()
    pagination_class = MyPageNumberPagination
    serializer_class = SpecOptionsSerializer

